package com.lsf.dunlian.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.google.gson.Gson;
import com.lsf.dunlian.R;
import com.lsf.dunlian.config.PreferenceUtil;
import com.lsf.dunlian.config.Urls;
import com.lsf.dunlian.constant.Constant;
import com.lsf.dunlian.entity.BaseEntity;
import com.lsf.dunlian.entity.LoginEntity;
import com.lsf.dunlian.entity.UserVO;

import org.apache.commons.lang3.StringUtils;

import java.io.IOException;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class LoginActivity extends AppCompatActivity {
    @BindView(R.id.tel)
    EditText tel;
    @BindView(R.id.pwd)
    EditText pwd;
    @BindView(R.id.loginBtn)
    Button loginBtn;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        BaseUtils.setStatusBar(this);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
        loginBtn.setOnClickListener(v -> login());
    }

    private void login() {
        if (StringUtils.isEmpty(tel.getText().toString())) {
            Toast.makeText(LoginActivity.this, "手机号不能为空!", Toast.LENGTH_SHORT);
            return;
        }
        if (StringUtils.isEmpty(pwd.getText().toString())) {
            Toast.makeText(LoginActivity.this, "密码不能为空!", Toast.LENGTH_SHORT);
            return;
        }
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder().get().url(Urls.LOGIN + tel.getText().toString() + "&pwd=" + pwd.getText().toString()).build();
        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                e.printStackTrace();
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response.isSuccessful()) {
                    String result = response.body().string();
                    LoginEntity entity = new Gson().fromJson(result, LoginEntity.class);

                    if (entity.code == 200) {
                        startActivity(new Intent(LoginActivity.this,MainActivity.class));
                        PreferenceUtil.putSharedPreference(Constant.USERID,entity.data.userId);
                        PreferenceUtil.putSharedPreference(Constant.NAME,entity.data.name);
                        PreferenceUtil.putSharedPreference(Constant.ICON,entity.data.icon);
                        PreferenceUtil.putSharedPreference(Constant.POSITIONNAME,entity.data.positionName);
                        finish();
                    } else {
                        runOnUiThread(() -> Toast.makeText(LoginActivity.this, entity.msg, Toast.LENGTH_SHORT));
                    }
                }
            }
        });
    }
}
